package com.cw.jeeyt.controller.system;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.system.SystemModuleInfoBo;
import com.cw.jeeyt.service.bo.system.SystemProjectInfoBo;
import com.cw.jeeyt.service.bo.system.SystemTemplateInfoBo;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.jeeyt.service.intf.system.SystemModuleInfoService;
import com.cw.jeeyt.service.intf.system.SystemProjectInfoService;
import com.cw.jeeyt.service.intf.system.SystemTemplateInfoService;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 项目信息控制类
 *
 * @author chenw
 * @version 1.0.0
 */
@Controller
@RequestMapping("/systemProjectInfo")
public class SystemProjectInfoController extends BaseController {
    @Resource
    private SystemProjectInfoService systemProjectInfoService;
    @Resource
    private SystemModuleInfoService systemModuleInfoService;
    @Resource
    private SystemTemplateInfoService systemTemplateInfoService;

    /**
     * 跳转到列表查询页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() throws Exception {
        ModelAndView mv = new ModelAndView("system/systemProjectInfo_index");
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 跳转到自动生成代码管理
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/generateCode")
    public ModelAndView generateCode() throws Exception {
        ModelAndView mv = new ModelAndView("system/systemProjectInfo_generateCode");
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 跳转到自动生成代码配置页面
     *
     * @param projectId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/generateCode_tree")
    public ModelAndView generateTree(String projectId) throws Exception {

        ModelAndView mv = new ModelAndView("system/generateCode_tree");
        StringBuilder nodesJson = new StringBuilder("[");
        SystemProjectInfoBo systemProjectInfoBo = systemProjectInfoService.getSystemProjectInfoById(projectId);
        if (systemProjectInfoBo != null) {
            nodesJson.append("{id:99, pId:0,moduleId:'', code:'" + systemProjectInfoBo.getProjectCode() + "',name: '"
                + systemProjectInfoBo.getProjectName() + "', open:true},");
            //根据项目id加载模块信息
            List<SystemModuleInfoBo> systemModuleInfoList
                = systemModuleInfoService.querySystemModuleInfoListByProjectId(projectId);
            if (systemModuleInfoList != null && !systemModuleInfoList.isEmpty()) {
                int i = 10;
                for (SystemModuleInfoBo smf : systemModuleInfoList) {
                    if (i == 10) {
                        nodesJson.append(
                            "{id:" + i + ",moduleId:'" + smf.getId() + "', pId:99, name: '" + smf.getModuleName()
                                + "',code:'" + smf.getModuleCode() + "', open:true}");
                    } else {
                        nodesJson.append(
                            ",{id:" + i + ",moduleId:'" + smf.getId() + "', pId:99, name: '" + smf.getModuleName()
                                + "',code:'" + smf.getModuleCode() + "', open:true}");
                    }
                    i++;
                }
            }
        }
        nodesJson.append("]");
        mv.addObject("nodesJson", nodesJson.toString());
        mv.addObject("systemProjectInfo", systemProjectInfoBo);
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listSystemProjectInfo")
    public ResultData listSystemProjectInfo(@RequestBody DataTable dataTable) throws Exception {

        SystemProjectInfoBo systemProjectInfoBo = JSONObject.parseObject(JSON.toJSONString(dataTable.getQueryCond()),
            SystemProjectInfoBo.class);
        if (systemProjectInfoBo == null) {
            systemProjectInfoBo = new SystemProjectInfoBo();
        }
        PageInfo<SystemProjectInfoBo> pageInfo = systemProjectInfoService.queryByPage(systemProjectInfoBo,
            dataTable.getStart(), dataTable.getLength());
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到表单页面
     *
     * @param id 主键
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView toForm(String id, String operation) throws Exception {

        ModelAndView mv = new ModelAndView("system/systemProjectInfo_form");
        SystemProjectInfoBo systemProjectInfoBo = null;
        SystemTemplateInfoBo systemTemplateInfoBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            systemProjectInfoBo = new SystemProjectInfoBo();
            systemTemplateInfoBo = new SystemTemplateInfoBo();
        } else {
            systemProjectInfoBo = systemProjectInfoService.getSystemProjectInfoById(id);
            systemTemplateInfoBo = systemTemplateInfoService.getSystemTemplateInfoById(
                systemProjectInfoBo.getTemplateId());
            if(systemTemplateInfoBo == null){
                systemTemplateInfoBo = new SystemTemplateInfoBo();
            }
            systemTemplateInfoBo.setTemplateName(
                systemTemplateInfoBo.getTemplateCode() + "---" + systemTemplateInfoBo.getTemplateName());
        }
        mv.addObject("systemProjectInfo", systemProjectInfoBo);
        mv.addObject("systemTemplateInfo", systemTemplateInfoBo);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        return mv;
    }

    /**
     * 根据操作类型保存数据
     *
     * @param systemProjectInfoBo
     * @param operation add新增  edit修改 view查看
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/saveSystemProjectInfo", method = RequestMethod.POST)
    public JsonResultDto saveSystemProjectInfo(SystemProjectInfoBo systemProjectInfoBo, String operation) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                systemProjectInfoService.insertSystemProjectInfo(systemProjectInfoBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                systemProjectInfoService.updateSystemProjectInfo(systemProjectInfoBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("保存失败:" + e.getMessage());

        }
        jsonResultDto.setOperation(operation);
        return jsonResultDto;
    }

    /**
     * 删除项目信息
     *
     * @param systemProjectInfoBo
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto deleteSystemProjectInfo(SystemProjectInfoBo systemProjectInfoBo) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            systemProjectInfoService.deleteSystemProjectInfo(systemProjectInfoBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("删除失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

}
